<template>
	<view>
		<view class="logo">
			<view class="img">
				<image mode="widthFix" src="/static/img/logo.png">
				</image>
			</view>
		</view>
		<!-- 账号密码输入框 -->
		<view class="form">


			<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhone">获取手机号</button>

		</view>
		<!-- 第三方登录 -->

	</view>
</template>

<script>
	import WXBizDataCrypt from "@/static/js/WXBizDataCrypt.js"
	export default {
		data() {
			return {
				phoneNumber: '',
				passwd: '',
				token: '',
				isShowOauth: false,
				showProvider: {
					weixin: false,
					qq: false,
					sinaweibo: false,
					xiaomi: false
				}
			}
		},
		onShow() {

		},
		onLoad(e) {
			this.token = e.token
		},
		methods: {
			getPhone(e) {
				console.log(e);
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					console.log("用户点击了接受")
					console.log(e.detail);
					let encryptedData = e.detail.encryptedData
					let errMsg = e.detail.errMsg
					let iv = e.detail.iv
					//获取session_key
					uni.login({
						success: (result) => {
							console.log(result)
							uni.request({
								url: getApp().globalData.data.requestUrl + "/Open/getSessionKey",
								data: {
									code: result.code,
								},
								success: (res) => {
									console.log(res)
									//获取手机号
									let pc = new WXBizDataCrypt(res.data.appid, res.data
										.session_key);
									let data = pc.decryptData(encryptedData, iv);
									console.log(data)
									let phone = data.phoneNumber
									console.log(phone)

									uni.request({
										url: getApp().globalData.data.requestUrl +
											"/Open/BindUser",
										data: {
											token: this.token,
											phone: phone
										},
										success: (res2) => {
											console.log(res2)
											if (res2.data.status == 1) {
												uni.setStorageSync('token', res2.data
													.data.token)
												uni.switchTab({
													url: '/pages/tabBar/user/user'
												})
											} else {
												uni.showToast({
													title: res2.data.message,
													icon: 'none'
												})
											}
										},
										fail: (err) => {
											console.log(err)


										}
									})
								}
							})

						}
					})
				} else {
					console.log("用户点击了拒绝")
				}

			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/css/login.scss";

	.form {
		.res {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100upx;
			color: rgba($color: #ffffff, $alpha: 0.8);
		}
	}

	.oauth {
		@media all and (max-height:150vw) {
			display: none;
		}

		position: absolute;
		bottom: 50upx;
		width: 100%;

		.text {
			width: 100%;
			height: 60upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: rgba($color: #ffffff, $alpha: 0.8);
			font-size: 28upx;
		}

		.list {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 20upx 0;

			.icon {
				font-size: 80upx;
				margin: 0 30upx;
			}
		}
	}
</style>
